<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : joinUserAndFeed.xsl
    Created on : June 10, 2013, 10:56 PM
    Author     : Thuc
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:param name="feedXML"/>
    <xsl:variable name="feed" select='document($feedXML)'></xsl:variable>
    <!-- TODO customize transformation rules 
         syntax recommendation http://www.w3.org/TR/xslt 
    -->
    <xsl:template match="/">
        <users xmlns="http://xml.netbeans.org/schema/user-feed">
            <xsl:for-each select="//user">
                <user>
                    <userId>
                        <xsl:value-of select='userId'/>
                    </userId>
                    <username>
                        <xsl:value-of select='username'/>
                    </username>
                    <fullName>
                        <xsl:value-of select='fullName'/>
                    </fullName>
                    <xsl:variable name="currentId" select='userId'></xsl:variable>
                    <numberOfFeed>
                        <xsl:value-of select="count($feed/feeds/feed/userlist[userId = $currentId])"/>
                    </numberOfFeed>
                    <feedlist>
                        <xsl:for-each select="$feed/feeds/feed[userlist/userId=$currentId]">
                            <feedtitle>
                                <xsl:value-of select='title'/>
                            </feedtitle>
                        </xsl:for-each>
                    </feedlist>
                </user>
            </xsl:for-each>
        </users>
    </xsl:template>

</xsl:stylesheet>
